package com.fingard.dsp.bank.directbank.gzelink01;

import java.util.Date;
import java.util.HashMap;
import java.util.Map;

import com.fingard.constant.Format;
import com.fingard.dsp.bank.directConfig.ActSetItem;
import com.fingard.dsp.bank.directbank.bankInterface.IGateway;
import com.fingard.dsp.bank.directbank.gzelink01.util.Md5;
import com.fingard.dsp.bank.reqrespobj.gateway.ReqGateway;
import com.fingard.dsp.bank.reqrespobj.unifiedorder.ReqQueryOrder;
import com.fingard.dsp.bank.reqrespobj.unifiedorder.RetAtsQueryOrder;
import com.fingard.text.StringHelper;
import com.fingard.util.HtmlStitchUtil;

public class GZELINK01Gateway extends GZELINK01Base implements IGateway {
	public HashMap<String, String> GenGwMap(ReqGateway p_reqGateway) throws Exception {
		HashMap<String, String> retMap = new HashMap<String, String>();

		try {
			String ownAct = p_reqGateway.reqHashMap.get("OurBankAccount");
			ActSetItem actItem = this.getBankConfig().getActSetItemByAny(ownAct, "", "", "");
			if (actItem != null) {
				String merId = actItem.merchantId;
				String orderNo = p_reqGateway.reqHashMap.get("RdSeq"); 
				String orderAmount = p_reqGateway.reqHashMap.get("Amount"); 
				String currCode = getBankConfig().getValueAsString("CurrCode"); 
				String orderType = "";
				String callBackUrl = actItem.notifyURL; 
				String bankCode = ""; 
				String langType = getCharset(); 
				String buzType = ""; 
				String reserved01 = p_reqGateway.reqHashMap.get("OurBankAccount") + "#" + p_reqGateway.reqHashMap.get("BankCode") + "#" + p_reqGateway.reqHashMap.get("CommandCode"); 
				String reserved02 = ""; 
				String signMsg = ""; 
				String encodePKey = Md5.md5(actItem.saltKey, langType);
				
				//组装订单原始数据
				StringBuffer waitSignData = new StringBuffer();
				
				waitSignData.append(merId);
				waitSignData.append(orderNo);
				waitSignData.append(orderAmount);
				waitSignData.append(currCode);
				waitSignData.append(orderType);
				waitSignData.append(callBackUrl);
				waitSignData.append(bankCode);
				waitSignData.append(langType);
				waitSignData.append(buzType);
				waitSignData.append(reserved01);
				waitSignData.append(reserved02);
				waitSignData.append(encodePKey);
				
				WriteBankLogLn2("广银联网关支付待签名数据:\r\n" + waitSignData.toString());
				
				// 执行签名
				signMsg = Md5.md5(waitSignData.toString(), langType);
				if (signMsg.length() > 0) {
					retMap.put("RespCode", "0");
					retMap.put("RespInfo", "签名成功");
					
					Map<String, String> reqFormMap = new HashMap<String, String>();
					
					reqFormMap.put("MerId", merId);
					reqFormMap.put("OrderNo", orderNo);
					reqFormMap.put("OrderAmount", orderAmount);
					reqFormMap.put("CurrCode", currCode);
					reqFormMap.put("OrderType", orderType);
					reqFormMap.put("CallBackUrl", callBackUrl);
					reqFormMap.put("BankCode", bankCode);
					reqFormMap.put("LangType", langType);
					reqFormMap.put("BuzType", buzType);
					reqFormMap.put("Reserved01", reserved01);
					reqFormMap.put("Reserved02", reserved02);
					reqFormMap.put("SignMsg", signMsg);
					
					String formHtmlStr = HtmlStitchUtil.joinTR(reqFormMap);
					String gatewayHtml = HtmlStitchUtil.buildHtml(formHtmlStr, bankFront.getUrl("gatewayPay"));
					
					retMap.put("Result", gatewayHtml);
				} else {
					retMap.put("RespCode", "-1");
					retMap.put("RespInfo", "签名失败");
				}
			} else {
				retMap.put("RespCode", "0");
				retMap.put("RespInfo", "未找到账号[" + ownAct + "]商户配置信息");
				retMap.put("Result", "");
			}
		} catch (Exception ex) {
			ex.printStackTrace();
			retMap.put("RespCode", "-1");
			retMap.put("RespInfo", ex.getMessage());
			WriteBankLogLn(ex);
		}

		return retMap;
	}

	public RetAtsQueryOrder queryGWPay(ReqQueryOrder p_reqGateway) throws Exception {
		RetAtsQueryOrder retAts = new RetAtsQueryOrder();
		retAts.rdSeq = p_reqGateway.rdSeq;
		
		try{
			ActSetItem actItem = this.getBankConfig().getActSetItemByAny(p_reqGateway.ownAct, p_reqGateway.merchantId, p_reqGateway.appId, "");
			if(actItem != null){
				String tranType = "100";
				String javaCharset = "UTF-8";
				String version = "V60";
				String userId = actItem.userCode;
				String pwd = Md5.md5(actItem.userFlag, javaCharset);
				String merId = actItem.merchantId;
				String payStatus = "2";
				String shoppingTime = Format.Date10Format.format(new Date()) + " 00:00:00";
				String beginTime = "";
				String endTime = "";
				String orderNo = "";
				if (StringHelper.hasAnyChar(p_reqGateway.rdSeq)) {
					orderNo = p_reqGateway.rdSeq;
				}
				
				//组装请求参数
				StringBuffer reqSendData = new StringBuffer();
				
				reqSendData.append("TranType=").append(tranType);
				reqSendData.append("&JavaCharset=").append(javaCharset);
				reqSendData.append("&Version=").append(version);
				reqSendData.append("&UserId=").append(userId); 
				reqSendData.append("&Pwd=").append(pwd);
				reqSendData.append("&MerId=").append(merId);
				reqSendData.append("&PayStatus=").append(payStatus);
				reqSendData.append("&ShoppingTime=").append(shoppingTime);
				reqSendData.append("&BeginTime=").append(beginTime);
				reqSendData.append("&EndTime=").append(endTime);
				reqSendData.append("&OrderNo=").append(orderNo);
				
				WriteBankLogLn2("2.广银联网关支付状态查询请求数据:\r\n" + reqSendData.toString());
				
				String url = bankFront.getUrl("gatewayPayQuery");
				WriteBankLogLn2("广银联网关支付状态查询服务地址:" + url);
				
				// 发送广银联
				String[] tmpRetStr = sendDataToGZE(reqSendData.toString(), url);
				
				if(tmpRetStr[0].length() == 0) { 
					WriteBankLogLn2("3.广银联网关支付状态查询响应数据:\r\n" + tmpRetStr[1]);
					
					if (tmpRetStr[1].length() > 0) {
						String[] respDetail = null;
						
						if (tmpRetStr[1].indexOf("&") > 0) {
							respDetail = tmpRetStr[1].split("&");
							
							retAts.respCode = "-1";
							retAts.respInfo = respDetail[1].split("=")[1];
						} else {
							respDetail = tmpRetStr[1].split("\\\\n");
							
							retAts.respCode = "0";
							retAts.respInfo = "";
							
							String rdSeq = respDetail[2];
							if (StringHelper.hasAnyChar(rdSeq)) {
								retAts.rdSeq = rdSeq;
							} else {
								retAts.rdSeq = p_reqGateway.rdSeq;
							}
							
							String status = respDetail[5];
							if (StringHelper.hasAnyChar(status)) {
								retAts.transState = respDetail[5];
								retAts.payInfoCode = respDetail[5];
							} else {
								// 未扫码查询不会返回状态，此时状态是等待买家付款
								retAts.transState = "4";
								retAts.payInfoCode = "WAIT_PAY";
							}
						}
					} else {
						retAts.respCode = "-1"; 
						retAts.respInfo = "广银联返回内容为空!";
					}
				} else {
					retAts.respCode = "-1"; 
					retAts.respInfo = tmpRetStr[1];
				}
			} else {
				retAts.respCode = "-1";
				retAts.respInfo = "未找到账号[" + p_reqGateway.ownAct + "]商户配置信息";
			}
		} catch(Exception ex) {
			ex.printStackTrace();
			retAts.respCode = "-1";
			retAts.respInfo = ex.getMessage();
			WriteBankLogLn(ex);
		}
		
		return retAts;
	}
}
